package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.EmployeeFile;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.EmployeeFileService;
import com.my.util.SessionUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工档案管理 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-08-29
 */
@Controller
@RequestMapping("/employeeFile")
public class EmployeeFileController {

    //注入
    @Autowired
	EmployeeFileService employeeFileService;

    //创建Logger日志对象
    Logger logger = Logger.getLogger(EmployeeFileController.class);


    /**
     * 返回员工的页面
     * @return 员工页面
     */
    @RequestMapping("/employee")
    public String employee(){
        return "employee";
    }

    /**
     * 添加员工
     * @param
     * @return 添加成功返回true  添加失败返回false
     */
    @RequestMapping("/insert")
    @ResponseBody
    public boolean insertEmployeeFile(String param,HttpSession session) {
		long areaid= ((User)session.getAttribute("user")).getAreaId();
        //使用反射把json格式的字符串转成对象
        EmployeeFile employeeFile = JSONObject.parseObject(param, EmployeeFile.class);
        employeeFile.setCreateTime(new Date().getTime());
        employeeFile.setAreaid(areaid);
        boolean flag = employeeFileService.insert(employeeFile);
        String msg = flag ? "添加成功" + employeeFile : "添加失败";
        logger.info(msg);
        return flag;

    }

    /**
     * 查询所有员工(分页查询)
     * @param voPage
     * @return 所有员工
     */
    @RequestMapping("/select")
    @ResponseBody
    public ResultPage<EmployeeFile> selectEmployeeFile(String lin, String data, VoPage voPage, HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
        long areaid= ((User)session.getAttribute("user")).getAreaId();
        ResultPage<EmployeeFile> list = employeeFileService.selectMyPage(map,voPage,areaid,admin);
        logger.info("list:"+list);
        return list;
    }

    /**
     * 修改员工的状态(1.在职 2.离职 3.出差 4.休假 5.请假)
     * @param
     * @return 员工信息
     */
    /*@RequestMapping("/update")
    @ResponseBody
    public boolean update(EmployeeFile employeeFile){

        return employeeFileService.updateById(employeeFile);
    }*/


    /**
     * 删除员工信息
     */
    @RequestMapping("/deleteEmployeeFile")
    @ResponseBody
    public boolean deleteEmployeeFile(Long id){
        return employeeFileService.deleteById(id);
    }

    /**
     * 修改员工信息
     * 页面用的是JSON格式的
     * 案例:
     * var jsonData={};
     * jsonData.name = $("#name").val();
     * @param param
     * @return
     */
    @RequestMapping("/updateEmployeeFile")
    @ResponseBody
    public String updateEmployeeFile(String param){
        EmployeeFile employeeFile = JSONObject.parseObject(param, EmployeeFile.class);
        /*List<EmployeeFile> list = new ArrayList<>();
        list.add(employeeFile);*/
        if(employeeFileService.updateById(employeeFile)){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    //返回添加页面
    @RequestMapping("/employee_add")
    public String employeeAdd(){
        return "/Alert/employee_add";
    }


    //返回编辑页面
    @RequestMapping("/employee_edit")
    public String employeeEdit(){
        return "/Alert/employee_edit";
    }


}
